﻿using APIService.Repository;
using APIService.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using APIService.Common;
using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;

namespace APIService.Controllers
{
    public class UserController : ApiController
    {
        private static UserRepository userRepository = new UserRepository();
        // GET api/user/Get
        public ReturnJsonInfo<List<TestUser>> Get()
        {
            ReturnJsonInfo<List<TestUser>> returnJson = new ReturnJsonInfo<List<TestUser>>();
           
            try
            {
                List<TestUser> userList = userRepository.Users.ToList();
                if (userList.Count == 0)
                {
                    returnJson.status = 1;
                    returnJson.message = "no data";
                    returnJson.data = null;
                }
                else
                {
                    returnJson.status = 1;
                    returnJson.message = "get data success";
                    returnJson.data = userList;
                }
            }
            catch (Exception error)
            {
                returnJson.status = 0;
                returnJson.message = error.Message;
                returnJson.data = null;
            }
            return returnJson;
        }

        // GET api/user/Get/5
        [RequireHttpsAttribute]
        public ReturnJsonInfo<TestUser> Get(int id)
        {
            ReturnJsonInfo<TestUser> returnJson = new ReturnJsonInfo<TestUser>();
            try
            {
                TestUser user = userRepository.Users.FirstOrDefault(t => t.Id == id);
                if (user == null)
                {
                    returnJson.status = 1;
                    returnJson.message = "no data";
                    returnJson.data = null;
                }
                else
                {
                    returnJson.status = 1;
                    returnJson.message = "get data success";
                    returnJson.data = user;
                }
            }
            catch (Exception error)
            {
                returnJson.status = 0;
                returnJson.message = error.Message;
                returnJson.data = null;
            }

            return returnJson;
        }

        // POST api/user/Save
        public ReturnJsonInfo<TestUser> Save(TestUser user)
        {
            ReturnJsonInfo<TestUser> returnJson = new ReturnJsonInfo<TestUser>();
            try
            {
                bool result = userRepository.Insert(user);
                if (result)
                {
                    returnJson.status = 1;
                    returnJson.message = "save data success";
                    returnJson.data = user;
                }
                else
                {

                    returnJson.status = 1;
                    returnJson.message = "fail to save data";
                    returnJson.data = null;
                }
            }
            catch (Exception error)
            {
                returnJson.status = 0;
                returnJson.message = error.Message;
                returnJson.data = null;
            }
            return returnJson;
        }

        // PUT api/user/Update
        public ReturnJsonInfo<TestUser> Update(TestUser user)
        {
            ReturnJsonInfo<TestUser> returnJson = new ReturnJsonInfo<TestUser>();
            try
            {
                bool result = userRepository.Update(user);
                //if updated data is same as old data, result is false
                returnJson.status = 1;
                returnJson.message = "update data success";
                returnJson.data = user;
            }
            catch (Exception error)
            {
                returnJson.status = 0;
                returnJson.message = error.Message;
                returnJson.data = null;
            }
            return returnJson;
        }

        // DELETE api/user/RemoveUser
        public ReturnJsonInfo<TestUser> RemoveUser(ParaInfo para)
        {
            ReturnJsonInfo<TestUser> returnJson = new ReturnJsonInfo<TestUser>();
            try
            {
                TestUser entity = userRepository.Users.FirstOrDefault(t => t.Id == para.Id);
                bool result = userRepository.Delete(entity);
                if (result)
                {
                    returnJson.status = 1;
                    returnJson.message = "delete data success";
                    returnJson.data = entity;
                }
                else
                {

                    returnJson.status = 0;
                    returnJson.message = "fail to delete data";
                    returnJson.data = null;
                }
            }
            catch (Exception error)
            {
                returnJson.status = 0;
                returnJson.message = error.Message;
                returnJson.data = null;
            }
            return returnJson;
        }

        /// <summary>
        /// validate user
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public ReturnJsonInfo<SecurityUser> ValidateUser(ValidatePara validatePara)
        {
            SecurityUserRepository securityUserRepository = new SecurityUserRepository();
            ReturnJsonInfo<SecurityUser> returnJson = new ReturnJsonInfo<SecurityUser>();
            try
            {
                SecurityUser entity = securityUserRepository.getSecurityUser(validatePara.Account);
                if (entity!=null)
                {
                    returnJson.status = 1;
                    returnJson.message = "get data success";
                    returnJson.data = entity;
                }
                else
                {

                    returnJson.status = 0;
                    returnJson.message = "no data";
                    returnJson.data = null;
                }
            }
            catch (Exception error)
            {
                returnJson.status = 0;
                returnJson.message = error.Message;
                returnJson.data = null;
            }
            return returnJson;
        }

        public string EncryptPassword(string password)
        {
            return Cryptographer.CreateHash("hashProvider", password);
        }
    }
}
